<template>

  <el-dialog
    top="20px"
    :title="defaultValue.title"
    :lock-scroll="defaultValue.lockScroll"
    :append-to-body="tableConfig.appendToBody"
    :visible="tableConfig.show"
    @close="tableConfig.show = false"
    :show-close="tableConfig.showClose"
    :destroy-on-close="tableConfig.destroyOnClose"
    width="1100px">

    <el-form ref="formBook" :rules="rulesWebsite" :model="formWebsite" label-width="180px">
      <el-form-item label="网站名称" prop="name" >
        <el-input v-model.trim="formWebsite.name"></el-input>
      </el-form-item>

      <el-form-item label="网址类型" prop="typeId">
        <el-select v-model="formWebsite.typeId" placeholder="请选择网址类型">
          <el-option
            v-for="item in websiteTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="网站名称" prop="url" >
        <el-input placeholder="请输入内容" v-model.trim="formWebsite.url">
          <template slot="prepend">URL</template>
        </el-input>
      </el-form-item>

      <el-form-item label="描述" prop="describe" >
        <el-input v-model.trim="formWebsite.describe"></el-input>
      </el-form-item>

      <el-form-item label="是否要翻墙" prop="overTheWall">
        <el-radio-group v-model="formWebsite.overTheWall">
          <el-radio :label="2">要翻墙</el-radio>
          <el-radio :label="1">不需要翻墙</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="是否常用" prop="commonlyUsed">
        <el-radio-group v-model="formWebsite.commonlyUsed">
          <el-radio :label="2">常用</el-radio>
          <el-radio :label="1">不常用</el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button plain type="warning" @click="toSaveUpd" v-if="defaultValue.saveUpdSelect !== 'select'">{{defaultValue.title}}</el-button>
      <el-button plain type="primary" @click="tableConfig.show = false">关闭</el-button>
    </span>

  </el-dialog>

</template>

<script>

  import {clearInvalid, getFullDate} from "../../utils/utils"
  import http from '@/utils/http'

  export default {
    name: "WebsiteSaveUpd",
    data(){
      return {
        // 默认值，进入页面后的值
        defaultValue: {
          // 弹出框标题
          title: '查看',
          // 保存还是修改or查看,取值有 select | save | upd
          saveUpdSelect: 'select'
        },
        // 表格的默认值
        tableConfig: {
          // 关闭时销毁 Dialog 中的元素
          destroyOnClose: true,
          // 弹出框是否打开
          show: false,
          // 是否显示关闭按钮
          showClose: true,
          // 下面2个属性解决打开弹出框后没有在最上面显示的问题，导致看到的内容是一半的，需要先将滚动条移到最上面
          lockScroll: false,
          appendToBody: true,
        },
        // 网站类型
        websiteTypeList: [],
        rulesWebsite: {
          name: [{ required: true, message: '请输入网站名称', trigger: 'blur' }],
          url: [{ required: true, message: '请输入网址', trigger: 'blur' }],
          typeId: [{ required: true, message: '请选择网址类型', trigger: 'change' }],
          overTheWall: [{ required: true, message: '请选择是否要翻墙', trigger: 'change' }],
          commonlyUsed: [{ required: true, message: '请选择是否常用', trigger: 'change' }],
        },
        // 要提交的数据
        formWebsite: {
          overTheWall: 1,
          name: null,
          url: null,
          numberOfClicks: null,
          commonlyUsed: 1,
          describe: null,
          typeId: null,
        },
      }
    },
    methods: {
      /**
       * 打开弹出框之前
       * @param value 保存还是修改or查看,取值有 select \ save \ upd
       * @param obj 如果是修改和查看，则是详细数据,需要赋值
       * @param type 网站类型
       */
      openPageWebsite(value, obj, type){
        // 从外部转入，减少查询
        this.websiteTypeList = type
        this.defaultValue.saveUpdSelect = value
        this.initWebsite()

        if (value === 'save'){
          this.defaultValue.title = '保存网址'

        } else if (value === 'upd'){
          this.defaultValue.title = '修改网址'
          this.formWebsite = JSON.parse(JSON.stringify(obj))

        } else {

        }

        this.tableConfig.show = true
        this.$store.commit('navigationListChange', ['我的网址', this.defaultValue.title])
      },
      /**
       * 是保存还是修改网址
       */
      toSaveUpd(){
        if (this.defaultValue.saveUpdSelect === 'save'){
          this.saveWebsite()

        } else if (this.defaultValue.saveUpdSelect === 'upd'){
          this.updWebsite()

        } else {

        }
      },
      /**
       * 保存网址信息
       */
      saveWebsite(){
        http.post("/web/saveWebsite", this.formWebsite).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.$message.success("保存成功");
          this.$emit('getWebList', null)
          // 关闭弹出框
          this.tableConfig.show = false

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 更新网址信息
       */
      updWebsite(){
        clearInvalid(this.formWebsite)
        http.post("/web/updWebsite", this.formWebsite).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.$message.success("修改成功");
          this.$emit('getWebList', null)
          // 关闭弹出框
          this.tableConfig.show = false

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 初始化网址信息
       */
      initWebsite(){
        this.formWebsite = {
          overTheWall: 1,
          name: null,
          url: null,
          numberOfClicks: null,
          commonlyUsed: 1,
          describe: null,
          typeId: null,
        }
      }
    }
  }
</script>

<style scoped>

</style>
